package com.view;

/**
 * 注册界面
 */

import com.DAL.DatabaseConnection;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class LogupView extends JFrame {
    JLabel titleLabel = new JLabel("界面注册", JLabel.CENTER);
    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JLabel userNameLabel = new JLabel("账号：");
    JTextField userTxt = new JTextField();
    JLabel pwdLabel = new JLabel("密码：");
    JPasswordField pwdTxt = new JPasswordField();
    JLabel pwdLabelA = new JLabel("确认密码：");
    JPasswordField pwdTxtA = new JPasswordField();
    JLabel phoneLabel = new JLabel("电话：");
    JTextField phoneTxt = new JTextField();
    JButton Confirm = new JButton(new AbstractAction("确认") {
        @Override
        public void actionPerformed(ActionEvent e) {
            String userName = userTxt.getText();
            String pwd = new String(pwdTxt.getPassword());
            String pwdA = new String(pwdTxtA.getPassword());
            String phone = phoneTxt.getText();
            String sql = "";
            Connection conn = DatabaseConnection.getConnection();
            if (userName.equals("") || pwd.equals("") || pwdA.equals("") || phone.equals("")) {
                JOptionPane.showMessageDialog(titleLabel, "输入的项不能为空", "警告", JOptionPane.WARNING_MESSAGE);
                return;
//            }else if (pwd != pwdA){
//                JOptionPane.showMessageDialog(titleLabel, "输入的密码不一致，请重新输入", "警告", JOptionPane.WARNING_MESSAGE);
//                return;
            }
            if (adminChoose.isSelected()) {
                //从数据库获取结果集
                try {
                    sql = "insert into admin(username,password,phone)values(?,?,?)";  //定义要执行的sql语句 占位符?表示一个动态参数
                    PreparedStatement preparedStatement = conn.prepareStatement(sql);
                    preparedStatement.setString(1, userName);
                    preparedStatement.setString(2, pwd);
                    preparedStatement.setString(3, phone);
                    preparedStatement.executeUpdate();
                    //TODO：如何解决此处短暂的卡顿
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        JOptionPane.showMessageDialog(titleLabel, "管理员：" + userName + "你好！" + "\n恭喜你注册成功！", "提示", JOptionPane.WARNING_MESSAGE);
                        dispose();
                        new LoginView();   //注册成功跳转到登陆界面
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            } else {
                //从数据库获取结果集
                try {
                    sql = "insert into owner(name,password,phone)values(?,?,?)";  //定义要执行的sql语句 占位符?表示一个动态参数
                    PreparedStatement preparedStatement = conn.prepareStatement(sql);
                    preparedStatement.setString(1, userName);
                    preparedStatement.setString(2, pwd);
                    preparedStatement.setString(3, phone);
                    preparedStatement.executeUpdate();
                    //TODO：如何解决此处短暂的卡顿
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        JOptionPane.showMessageDialog(titleLabel, "用户：" + userName + "你好！" + "\n恭喜你注册成功！", "提示", JOptionPane.WARNING_MESSAGE);
                        dispose();
                        new LoginView();   //注册成功跳转到登陆界面
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    });
    JButton Reset = new JButton(new AbstractAction("重置") {
        @Override
        public void actionPerformed(ActionEvent e) {
            userTxt.setText("");
            pwdTxt.setText("");
            pwdTxtA.setText("");
            phoneTxt.setText("");
        }
    });
    JButton Back = new JButton(new AbstractAction("返回") {
        @Override
        public void actionPerformed(ActionEvent e) {
            new LoginView();
            dispose();
        }
    });
    JLabel Choose = new JLabel("用户类型：");
    JRadioButton adminChoose = new JRadioButton("物业管理员");
    JRadioButton ownerChoose = new JRadioButton("小区业主", true);
    JLabel RemindBar = new JLabel("温馨提醒：物业管理员注册账户需取得注册码");

    public LogupView() {
        //TODO：将每个窗口的标题提取至一处
        super("注册界面");

        Container contentPane = getContentPane();

        //设置字体
        setFont();

        //把组件加入面板
        contentPane.add(titleLabel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdTxt);
        centerPanel.add(pwdLabelA);
        centerPanel.add(pwdTxtA);
        centerPanel.add(phoneLabel);
        centerPanel.add(phoneTxt);
        centerPanel.add(Confirm);
        centerPanel.add(Reset);
        centerPanel.add(Back);
        contentPane.add(RemindBar, BorderLayout.SOUTH);

        ButtonGroup group = new ButtonGroup();
        centerPanel.add(Choose);
        group.add(this.adminChoose);
        group.add(this.ownerChoose);
        centerPanel.add(this.adminChoose);
        centerPanel.add(this.ownerChoose);

        //设置弹簧布局
        springLayout(contentPane);

        //设置窗口图标
        setIcon();

        setSize(600, 800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    //字体及大小设置 Method
    private void setFont() {
        titleLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        titleLabel.setPreferredSize(new Dimension(0, 190));

        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(centerFont);
        pwdTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabelA.setFont(centerFont);
        pwdTxtA.setPreferredSize(new Dimension(200, 30));
        phoneLabel.setFont(centerFont);
        phoneTxt.setPreferredSize(new Dimension(200, 30));
        Confirm.setFont(centerFont);
        Reset.setFont(centerFont);
        Back.setFont(centerFont);
        Choose.setFont(centerFont);
        adminChoose.setFont(centerFont);
        ownerChoose.setFont(centerFont);
        RemindBar.setFont(centerFont);
        RemindBar.setBorder(new CompoundBorder(new LineBorder(Color.WHITE),
                new EmptyBorder(4, 4, 4, 4)));
    }

    //弹簧布局 Method
    private void springLayout(Container contentPane) {
        //布局userNameLabel
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userTxt)),
                Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;

        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX,
                SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);
        //布局userTxt
        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, userNameLabel);
        //布局pwdLabel
        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 40, SpringLayout.SOUTH, userNameLabel);
        //布局pwdTxt
        springLayout.putConstraint(SpringLayout.WEST, pwdTxt, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdTxt, 0, SpringLayout.NORTH, pwdLabel);
        //布局pwdLabelA
        springLayout.putConstraint(SpringLayout.EAST, pwdLabelA, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabelA, 40, SpringLayout.SOUTH, pwdLabel);
        //布局pwdTxtA
        springLayout.putConstraint(SpringLayout.WEST, pwdTxtA, 20, SpringLayout.EAST, pwdLabelA);
        springLayout.putConstraint(SpringLayout.NORTH, pwdTxtA, 0, SpringLayout.NORTH, pwdLabelA);
        //布局phoneLabel
        springLayout.putConstraint(SpringLayout.EAST, phoneLabel, 0, SpringLayout.EAST, pwdLabelA);
        springLayout.putConstraint(SpringLayout.NORTH, phoneLabel, 40, SpringLayout.SOUTH, pwdLabelA);
        //布局phoneTxt
        springLayout.putConstraint(SpringLayout.WEST, phoneTxt, 20, SpringLayout.EAST, phoneLabel);
        springLayout.putConstraint(SpringLayout.NORTH, phoneTxt, 0, SpringLayout.NORTH, phoneLabel);
        //布局Confirm
        springLayout.putConstraint(SpringLayout.EAST, Confirm, -40, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, Confirm, 210, SpringLayout.SOUTH, pwdLabel);
        //布局Reset
        springLayout.putConstraint(SpringLayout.EAST, Reset, 160, SpringLayout.EAST, Confirm);
        springLayout.putConstraint(SpringLayout.NORTH, Reset, 0, SpringLayout.NORTH, Confirm);
        //布局Back
        springLayout.putConstraint(SpringLayout.EAST, Back, 160, SpringLayout.EAST, Reset);
        springLayout.putConstraint(SpringLayout.NORTH, Back, 0, SpringLayout.NORTH, Confirm);
        //布局Choose
        springLayout.putConstraint(SpringLayout.EAST, Choose, 40, SpringLayout.WEST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, Choose, 80, SpringLayout.SOUTH, Confirm);
        //布局adminChoose
        springLayout.putConstraint(SpringLayout.WEST, adminChoose, 40, SpringLayout.EAST, Choose);
        springLayout.putConstraint(SpringLayout.NORTH, adminChoose, -5, SpringLayout.NORTH, Choose);
        //布局ownerChoose
        springLayout.putConstraint(SpringLayout.WEST, ownerChoose, 40, SpringLayout.EAST, adminChoose);
        springLayout.putConstraint(SpringLayout.NORTH, ownerChoose, -5, SpringLayout.NORTH, Choose);

        SpringLayout.Constraints titleLabelC = springLayout.getConstraints(titleLabel);
//        titleLabelC.setX(Spring.constant(100));
        titleLabelC.setY(Spring.constant(50));

    }

    //设置窗体图标 Method
    private void setIcon() {
//        URL resource = FirstScene.class.getClassLoader().getResource("Icon.jpg");
//        Image image = new ImageIcon(resource).getImage();
//        setIconImage(image);
        Image icon = Toolkit.getDefaultToolkit().getImage("src/resource/Icon.jpg");
        this.setIconImage(icon);
    }

    //Main方法
    public static void main(String[] args) {
        new LogupView();
    }

}
